<?php

class News extends BaseNews
{		
	public function delete(PropelPDO $con = null)
   {
    	parent::delete($con);
		
		if(file_exists(sfConfig::get('sf_upload_dir').'/assets/news/'.utilities::getSpreadPathByDate($this->getCreatedAt()).'/'.$this->getThumb())) {
    		unlink(sfConfig::get('sf_upload_dir').'/assets/news/'.utilities::getSpreadPathByDate($this->getCreatedAt()).'/'.$this->getThumb());
			unlink(sfConfig::get('sf_upload_dir').'/assets/news/'.utilities::getSpreadPathByDate($this->getCreatedAt()).'/thumb_'.$this->getThumb());
			//$datetime = date_create($this->getCreatedAt());
			//$pathDelete = sfConfig::get('sf_upload_dir').'/assets/news/'.date_format($datetime, 'Y/m');
			//utilities::deleteAll($pathDelete, true);
		}
				
   }
   
   public function save(PropelPDO $con = null) 
  {
  	//sfContext::createInstance($this->configuration);
	$request = sfContext::getInstance()->getRequest();
	//$response = sfContext::getInstance()->getResponse();  	
	$con->beginTransaction();
	try
	{
		$res = parent::save($con);
		$con->commit();
		
		$insertedId = parent::getId();
		
		if (!$this->isNew())
		{
			$obj_2 = DbFinder::from('NewsTags')
								->where('NewsId', $insertedId)
								->delete();
		}
		
		$tags_list = $request->getParameter('news');
		$a = explode(',',$tags_list['tags']);
		foreach($a as $value) {
			if(trim($value)!='') {
				$tags_id = Tags::insertTag($value);
				NewsTags::addNewsTags($insertedId, $tags_id);
			}
		}		
		return $res;
	} catch (Exception $e) {
		$con->rollBack();
		throw $e;
	}
  }
  
}
